package com.budwk.app.sqfw.services.datamange.impl;

import com.budwk.app.sqfw.models.*;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfw.services.datamange.DataMangeCityService;
import com.budwk.app.sqfw.services.datamange.DataMangeCommunityService;
import com.budwk.app.sqfw.vo.SqfwDataMangeVO;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.database.service.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

@Slf4j
@IocBean(args = {"refer:dao"})
public class DataMangeCityServiceImpl extends BaseServiceImpl<Sqfw_datamange_city> implements DataMangeCityService {
    public DataMangeCityServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public Pagination page(SqfwDataMangeVO bean) {
        if (null == bean) {
            bean = new SqfwDataMangeVO();
        }
        long startTime = System.currentTimeMillis();
        // 构造查询条件
        Criteria criteria = buildQueryCond(bean);
        log.info("DataMangeStreetCheckServiceImpl-list-SQL: {}", criteria.toString());
        Pagination pagination = this.listPage(bean.getPageNo(), bean.getPageSize(), criteria);
        log.info("DataMangeStreetCheckServiceImpl-list-耗时: {}ms", System.currentTimeMillis() - startTime);
        return pagination;
    }

    /**
     * 构造查询条件
     *
     * @param bean
     * @return
     */
    private Criteria buildQueryCond(SqfwDataMangeVO bean) {
        Criteria cri = Cnd.cri();
        SqlExpressionGroup criSql = cri.where();
        // 不查询excel头
        criSql.andNotEquals(Sqfw_datamange_community::getExcelSeg, "header");
        criSql.andEquals(Sqfw_datamange_community::getDelFlag, false);
        if (null != bean && StringUtils.isNotEmpty(bean.getPlaceType())) {
            criSql.andLike("param_data->>'placeType'", bean.getPlaceType());
        }
        if (null != bean && StringUtils.isNotEmpty(bean.getCommunity())) {
            criSql.andLike(Sqfw_datamange_community::getCommunity, bean.getCommunity());
        }
        return cri;
    }
}
